<?php

/**
 * This is the model class for table "{{mem_history}}".
 *
 * 会员浏览模型
 * 张梦林
 * 2012-11-29
 * 接口方法一：
 * 添加浏览记录,需传入$zp_id产品ID
 * MemHistory::model()->createBrowsingHistory($zp_id);
 * 
 * 接口方法二：
 * 保存浏览记录，用户出的时候执行此操作
 * MemHistory::model()->saveBrowsingHistory();
 * 
 * 接口方法三：
 * 查找浏览记录。返回用户最近浏览记录
 * MemHistory::model()->readBrowsingHistory();
 */
class MemHistory extends CActiveRecord
{
	//定义浏览记录表最多记录的最近浏览产品的条数
	private $maxRecordNum = 5;
	//定义cookie里面存放的会员ID变量
	private $cookidMemberID = 'MemberID';
	//调试用会员ID，待于null时关闭调试
	private $userid = null;
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return MemHistory the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{mem_history}}';
	}
	
	/**
	 * @return array 关联规则.
	 */
	public function relations()
	{
		return array(
				'product'=>array(self::HAS_ONE, 'Product', 'zp_id'),
		);
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		return array(
			array('zmh_id, zm_id, zp_id,zmh_listorder, inputtime', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * 生成浏览记录
	 * 载入产品页面调用此方法
	 * @param $zp_id	//浏览的产品ID
	 * */
	public function createBrowsingHistory($zp_id)
	{
		//取出cookie里存储的浏览记录
		$cookie = Yii::app()->request->getCookies();
		if($cookie['bhistory']){
			if($cookie['bhistory']->value){
				$BHistory = unserialize($cookie['bhistory']->value);
				if(!is_array($BHistory)){
					$BHistory = array('0'=>$BHistory);
				}
			}else{
				$BHistory = array();
			}
		}else{
			$BHistory = array();
		}
		//将新建产品ID放在最前
		array_unshift($BHistory,$zp_id);
		//去掉重复的值
		$BHistory = array_unique($BHistory);
		//重新生成索引
		$BHistory = updateKeyForArray($BHistory);
		//如果个数大于最大数，踢出最后一个
		if(count($BHistory)>$this->maxRecordNum){
			$BHistory = array_chunk($BHistory,$this->maxRecordNum);
			$BHistory = $BHistory[0];
		}
		//将数组系列化这后保存到COOKIE
		$BHistory = serialize($BHistory);
		$cookie = new CHttpCookie('bhistory',$BHistory);
		$cookie->expire = time()+60*60*24*30;  //有限期30天
		Yii::app()->request->cookies['bhistory']=$cookie;
	}
	
	/**
	 * 保存浏览记录到数据库
	 * 用户退出登录时调用此操作
	 * */
	public function saveBrowsingHistory()
	{
		$cookie = Yii::app()->request->getCookies();
		if($cookie['bhistory']){
			if($cookie['bhistory']->value){
				$BHistory = unserialize($cookie['bhistory']->value);
				if(!is_array($BHistory)){
					$BHistory = array('0'=>$BHistory);
				}
			}else{
				$BHistory = array();
			}
		}else{
			$BHistory = array();
		}
	
		//获取会员ID
		$zm_id = $this->getUserId();
		
		//COOKIE里存储的浏览记录不够记录数量，补全到记录数量
		if(count($BHistory)<$this->maxRecordNum && $zm_id != null){
			$criteria = new CDbCriteria;
			$criteria->select ='zmh_listorder,zp_id';
			$criteria->condition = 'zm_id='.$zm_id;
			$criteria->order = 'zmh_listorder ASC';
			$criteria->limit = $this->maxRecordNum-count($BHistory);
			$fromBHistory=$this->findAll($criteria);
		
			//将查询的数据转换成数组
			if(!empty($fromBHistory)){
				$fromBHistory = simplifyData($fromBHistory);
				foreach($fromBHistory as $key=>$val){
					$recordHistory[$val['zmh_listorder']] = $val['zp_id'];
				}
				//合并数据
				$BHistory = array_merge($BHistory,$recordHistory);
			}
			//重新生成索引
			$BHistory = updateKeyForArray($BHistory);
		}
		//删除会员浏览记录表旧的数据记录
		if($zm_id != null){
			$condidtions = 'zm_id='.$zm_id;
			$this->deleteAll($condidtions,'*');
		}
		
		foreach($BHistory as $key=>$value){
			$this->__construct('insert');
			$this->zmh_id = '';
			$this->zm_id = $zm_id;
			$this->zp_id = $value;
			$this->zmh_listorder = $key;
			$this->inputtime = time();
			$data = array('zmh_id','zm_id','zp_id','zmh_listorder','inputtime');
			$this->insert($data);
		}
	
		unset($cookie['bhistory']);
	}
	
	/**
	 * 读取最近浏览记录
	 * return 返回用户最新的十条浏览记录
	 * */
	public function readBrowsingHistory(){
		//读取cookice存的记录
		$cookie = Yii::app()->request->getCookies();
		//将记录反系列化
		if($cookie['bhistory']){
			if($cookie['bhistory']->value){
				$BHistory = unserialize($cookie['bhistory']->value);
				if(!is_array($BHistory)){
					$BHistory = array('0'=>$BHistory);
				}
			}else{
				$BHistory = array();
			}
		}else{
			$BHistory = array();
		}

		//获取会员ID
		$zm_id = $this->getUserId();

		//COOKIE里存储的浏览记录不够记录数量，补全到记录数量
		if(count($BHistory)<$this->maxRecordNum && $zm_id != null){
			$criteria = new CDbCriteria;
				$criteria->select ='zmh_listorder,zp_id';
				$criteria->condition = 'zm_id='.$zm_id;
				$criteria->order = 'zmh_listorder ASC';
				$criteria->limit = $this->maxRecordNum-count($BHistory);
				$fromBHistory=$this->findAll($criteria);
				
			//将查询的数据转换成数组
			if(!empty($fromBHistory)){
				$fromBHistory = simplifyData($fromBHistory);
				foreach($fromBHistory as $key=>$val){
					$recordHistory[$val['zmh_listorder']] = $val['zp_id'];
				}
				//合并数据
				$BHistory = array_merge($BHistory,$recordHistory);
			}
			//重新生成索引
			sort($BHistory);
			return $BHistory;
		}else{
			return $BHistory;
		}
	}

	/**
	 * 获取会员存储在cookie里面的ID
	 * */
	private function getUserId(){
		$cookie = Yii::app()->request->getCookies();
		if(isset(Yii::app()->user->usertype)&&Yii::app()->user->usertype=="member"){
			return Yii::app()->user->getId();
		}else{
			$zm_id = NULL;
		}
		if(isset($this->userid)){
			$zm_id = $this->userid;
		}
		return $zm_id;
	}
}